<?php
namespace app\common\controller\services;

use app\common\controller\Api;
use think\Db;

class User extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 用户解除绑定空间
     * $orderId 订单ID
     * $storeId 空间/中心ID
     */
    public function unBindStore($userId)
    {
        try {
            $user = db('user')->where('id',$userId)->find();
            $orderIng = db('order')->where(['user_id'=>$userId])->where('order_status','in',[1,2,3])->find();
            if (!empty($orderIng)) {
                return ['code'=>201,'msg'=>'您有进行中的订单，完成订单后再进行解除绑定操作'];
            }
            $integralOrderIng = db('integral_order')->where(['user_id'=>$userId,'order_status'=>1])->find();
            if (!empty($integralOrderIng)) {
                return ['code'=>201,'msg'=>'您有进行中的积分订单，完成订单后再进行解除绑定操作'];
            }
            $withdrawIng = db('withdraw')->where(['status'=>1,'user_id'=>$userId,'user_del'=>'0'])->find();
            if (!empty($withdrawIng)) {
                return ['code'=>201,'msg'=>'您有申请中的提现，待提现审核后再进行解除绑定操作'];
            }
            //清空用户积分、身份（合伙人、高级用户）、分销关系、推广关系，以及未提现的佣金、分红；
            db('user')->where('id',$userId)->update([
                'store_id'=>0,
                'integral'=>0,
                'type'=>1,
                'pid'=>0,
                'two_pid'=>0,
                'is_card_pay'=>0,
                'card_pay_time'=>0,
                'storage_balance'=>0,
                'end_time'=>0,
                'first_storage_balance'=>0,
                'bonus_balance'=>0,
                'promotion_balance'=>0,
                'promotion_money'=>0,
                'promotion_type'=>0,
                'promotion_id'=>0,
                'commission_money'=>0,
                'xcx_qrcode'=>'',
                'edittime'=>time(),
            ]);

            if (in_array($user['type'],[2,3])) {
                //直接下级去除分销绑定关系
                db('user')->where('pid',$userId)->update([
                    'pid'=>0,
                    'two_pid'=>0
                ]);

                //二级下级去除分销绑定关系
                db('user')->where('two_pid',$userId)->update([
                    'two_pid'=>0,
                ]);

                //推广关系解除
                if ($user['type'] == 3) {
                    //推广合伙人关系解除
                    db('user')->where(['promotion_type'=>3,'promotion_id'=>$userId])->update([
                        'promotion_type'=>0,
                        'promotion_id'=>0
                    ]);

                    //推广中心/空间关系解除
                    db('store')->where(['promotion_type'=>3,'promotion_id'=>$userId])->update([
                        'promotion_type'=>0,
                        'promotion_id'=>0
                    ]);
                }
            }
            if ($user['type'] == 3) {
                //合伙人解绑所有收益停止发放
                db('order_goods_store')->where(['promotion_id'=>$userId,'promotion_type'=>3,'isfa'=>0])->update([
                    'promotion_price_pt'=>0,
                    'promotion_rate'=>0,
                    'promotion_id'=>0,
                    'promotion_type'=>0
                ]);
                db('order_goods_store')->where(['p_user_id'=>$userId,'successtime'=>0])->where('order_id','>',0)->update([
                    'fx_price'=>0,
                    'fx_rate'=>0
                ]);
                db('order_goods_store')->where(['p_user_idtwo'=>$userId,'successtime'=>0])->where('order_id','>',0)->update([
                    'fx_pricetwo'=>0,
                    'fx_ratetwo'=>0
                ]);

                //合伙人推合伙人
                db('order_goods_store')->where(['promotion_type_partner'=>3,'promotion_id_partner'=>$userId,'successtime'=>0])->where('order_id','>',0)->update([
                    'promotion_price'=>0,
                    'promotion_rate_partner'=>0,
                    'promotion_type_partner'=>0,
                    'promotion_id_partner'=>0
                ]);
            }


            if ($user['type'] == 2) {
                //高级用户
                db('order_goods_store')->where(['p_user_id'=>$userId,'successtime'=>0])->where('order_id','>',0)->update([
                    'fx_price'=>0,
                    'fx_rate'=>0
                ]);
                db('order_goods_store')->where(['p_user_idtwo'=>$userId,'successtime'=>0])->where('order_id','>',0)->update([
                    'fx_pricetwo'=>0,
                    'fx_ratetwo'=>0
                ]);
            }


            //将所有记录可查看状态进行更改
            db('fenxiao_log')->where('user_id',$userId)->update([
                'user_del'=>1
            ]);
            db('integral_change_log')->where('user_id',$userId)->update([
                'user_del'=>1
            ]);
            db('promition_log')->where('promotion_id',$userId)->update([
                'user_del'=>1
            ]);
            db('card_log')->where(['type'=>3,'user_id'=>$userId])->update([
                'user_del'=>1
            ]);
            db('card_pay')->where('user_id',$userId)->update([
                'user_del'=>1
            ]);
            db('order_bonus')->where('promotion_id',$userId)->update([
                'user_del'=>1
            ]);
            db('withdraw')->where('user_id',$userId)->update([
                'user_del'=>1
            ]);
            db('cart')->where('user_id',$userId)->update([
                'user_del'=>1
            ]);

            db('apply')->where('user_id',$userId)->where('type',3)->update([
                'user_del'=>1
            ]);
            return ['code'=>200,'msg'=>'解除成功'];
        } catch (ValidateException|PDOException|Exception $e) {
            return ['code'=>201,'msg'=>$e->getMessage()];
            $this->error($e->getMessage());
        }
    }
}
